﻿{include file='pub/header'/}
<title>产品分类</title>
</head>
<body>
<nav class="breadcrumb">{$breadNav} <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
                                       href="javascript:location.replace(location.href);" title="刷新"><i
        class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <div class="cl pd-5 bg-1 bk-gray mt-20">
        <span class="l">
            <a href="javascript:;" onclick="datadel()" class="btn btn-danger radius">
                <i class="Hui-iconfont">&#xe6e2;</i> 批量删除
            </a>
            <a href="javascript:;" onclick="asyncCategory()" class="btn btn-danger radius">
                <i class="Hui-iconfont">&#xe641;</i> 同步分类
            </a>
        </span>
    </div>
    <div class="mt-20">
        <table class="table table-border table-bordered table-bg table-hover table-sort">
            <thead>
            <tr class="text-c">
                <th width="40"><input name="" type="checkbox" value=""></th>
                <th width="80">ID</th>
                <th width="100">分类名称</th>
                <th width="100">图片</th>
                <th width="100">显示权重</th>
                <th width="100">操作</th>
            </tr>
            </thead>
            <tbody>
            {foreach $cateList as $list}
            <tr class="text-c">
                <td><input name="ids[]" type="checkbox" value="{$list.id}"></td>
                <td>{$list.id}</td>
                <td>{$list.name}</td>
                <td><img height="45px;" class="picture-thumb" src="{$list.img}"
                         onclick="picture_show('{$list.name}', '{$list.img}')"></td>
                <td>{$list.ord_id}</td>
                <td class="td-manage">
                    <a style="text-decoration:none" class="ml-5" onClick="set_order('{$list.id}', '{$list.ord_id}')" href="javascript:;" title="权重设置">
                        <i class="Hui-iconfont">&#xe61d;</i>权重设置
                    </a>
                    <a style="text-decoration:none" class="ml-5" onClick="category_child('{$list.name}','{:url(\'Products/cateDet\', [\'parent_id\'=>$list.id])}')" href="javascript:;" title="编辑">
                        <i class="Hui-iconfont">&#xe681;</i>查看子分类
                    </a>
                    {if $list.is_recommend == 0}
                    <a style="text-decoration:none" class="ml-5" onClick="set_recommend('{$list.id}', 1)" href="javascript:;">
                        <i class="Hui-iconfont">&#xe681;</i>设为首页推荐
                    </a>
                    {else/}
                    <a style="text-decoration:none" class="ml-5" onClick="set_recommend('{$list.id}', 0)" href="javascript:;">
                        <i class="Hui-iconfont">&#xe681;</i>取消首页推荐
                    </a>
                    {/if}
                    <a style="text-decoration:none" class="ml-5" onClick="category_del(this,'{$list.id}')" href="javascript:;" title="删除">
                        <i class="Hui-iconfont">&#xe6e2;</i>删除
                    </a>
                </td>
            </tr>
            {/foreach}
            </tbody>
        </table>
    </div>
</div>

{include file="pub/footer"/}
<script type="text/javascript">
    $('.table-sort').dataTable();

    function set_order(cate_id, default_ord){
        layer.prompt({
            title:'请输入排序数值，数值越大显示越靠前',
            value:default_ord,
            formType:0
        }, function (val) {
            if(val == default_ord){

            }else{
                $.post("{:url('set_order')}", {cate_id:cate_id, ord_id:val}, function (res) {
                    if(res.code == 1){
                        layer.msg('修改成功');
                        location.reload();
                    }
                });
            }
            layer.closeAll();
        })
    }

    //设置首推
    function set_recommend(cate_id, status){
        var msg;
        if(status == 1){
            msg = '确定设为首页推荐？';
        }else{
            msg = '确定取消首页推荐？';
        }
        layer.confirm(msg, function () {
            layer.load();
            $.post('{:url("Products/setCateRecommend")}', {cate_id:cate_id, recommend:status}, function (res) {
                if(res.code == 1){
                    location.reload();
                }else{
                    layer.close();
                    layer.msg(res.msg);
                }
            })

        })
    }
    //同步分类
    function asyncCategory() {
        layer_show('同步分类', "{:url('Products/asyncCate')}")

    }

    //批量删除分类
    function datadel() {
        var idNode = $("tbody input[type='checkbox']:checked");
        var ids = '';
        $.each(idNode, function (i, v) {
            ids += $(v).val() + ",";
        });
        ids = ids.substr(0, ids.length - 1);
        layer.confirm('确定删除选中分类，并同时删除当前分类下的子分类？', function (res) {
            $.post('{:url("Products/delCate")}', {ids: ids}, function (res) {
                if (res.code == 1) {
                    idNode.parent().parent().remove();
                    layer.msg('操作成功', {icon: 6, time: 1000});
                } else {
                    layer.msg('操作失败');
                }
            });
        });

    }

    //查看子分类
    function category_child(title, url) {
        layer_show(title, url);
    }

    /*分类图片-查看*/
    function picture_show(title, url, id) {
        var index = layer.open({
            type: 1,
            title: title,
            area: ['300px', '300px'],
            content: "<div style='width: 280px; text-align: center; margin: auto;'> <img src='" + url + "' style='width: 180px;'></div>"
        });
        // layer.full(index);
    }

    /*分类-删除*/
    function category_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $.ajax({
                type: 'POST',
                url: '{:url("Products/delCate")}',
                data: {ids: id},
                dataType: 'json',
                success: function (data) {
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!', {icon: 1, time: 1000});
                },
                error: function (data) {
                    console.log(data.msg);
                },
            });
        });
    }
</script>
</body>
</html>